Given two binary strings, return their sum (also a binary string).

The input strings are bothnon-emptyand contains only characters1or 0.

Example 1:

Input:
 a = "11", b = "1"

Output:
 "100"

Example 2:

Input:
 a = "1010", b = "1011"

Output:
 "10101"

class Solution {

public:

string addBinary\(string a, string b\) {

    string s="";

    int i=a.size\(\)-1; int j=b.size\(\)-1;

    int c=0;



    while\(i>=0\|\|j>=0\|\|c>0\){

        c+=i>=0?a\[i--\]-'0':0;

        c+=j>=0?b\[j--\]-'0':0;

        s=char\(c%2+'0'\)+s;

        c=c/2;

    }



    return s;

}

};

results matching ""

    No results matching ""